# -*- coding: utf-8 -*-

from PyQt4 import QtGui,QtCore
from logon_dlg import Ui_logon_dlg
from logic import thread_msgs

class LogonUI(QtCore.QObject):
    def __init__(self, logicthread):
        QtCore.QObject.__init__(self)
        self.logicthread = logicthread
        self.dlg = QtGui.QDialog()
        self.logon_dlg = Ui_logon_dlg()
        self.logon_dlg.setupUi(self.dlg)
        self.dlg.setModal(False)
        flags=QtCore.Qt.WindowMinMaxButtonsHint 
        self.dlg.setWindowFlags(flags)      
        
        self.dlg.show()
        self.logon_dlg.combo_username.lineEdit().setText(u"")
        self.logon_dlg.edit_password.setText(u'')
        self.logon_dlg.edit_deviceid.setText(u'moblove_pytest_100')
        self.logon_dlg.btn_logon.clicked.connect(self.on_logon_clicked)
        self.logon_dlg.combo_username.activated.connect(self.on_choose_user_item)
        QtCore.QObject.connect(self,QtCore.SIGNAL("logic_logon_success_sig()"), self.logic_logon_success_slot)
        QtCore.QObject.connect(self,QtCore.SIGNAL("logic_logon_failed_sig()"), self.logic_logon_failed_slot)        
        self.logon_dlg.btn_register.clicked.connect(self.on_register_clicked)
        self.cfgusers = None
                
    def on_logon_clicked(self):
#        key = str(self.logon_dlg.combo_username.lineEdit().displayText())
#        if (self.cfgusers.has_key( key )):
#            userinfo = self.cfgusers[key]
#            username = userinfo['username']
#        else:
#            username = str(self.logon_dlg.combo_username.lineEdit().displayText()) 
#        password = self.logon_dlg.edit_password.displayText().toUtf8()
#        deviceid = self.logon_dlg.edit_deviceid.displayText().toUtf8()
#        self.logicthread.post_msg(thread_msgs.MsgLogon(username, password, deviceid))
        #self.logon_dlg.btn_logon.setText(u"断开")
        #self.logon_dlg.btn_logon.setEnabled(False)
        deviceid = self.logon_dlg.edit_deviceid.displayText().toUtf8()
        self.logicthread.post_msg(thread_msgs.MsgLogon("","",deviceid))
        
    def on_register_clicked(self):
        deviceid = self.logon_dlg.edit_deviceid.displayText().toUtf8()
        self.logicthread.post_msg(thread_msgs.MsgRegister(deviceid))
        
    def logic_logon_success(self):
        #self.emit(QtCore.SIGNAL('logic_logon_success_sig()'))
        pass        
    
    def logic_logon_failed(self):
        #self.emit(QtCore.SIGNAL('logic_logon_failed_sig()'))
        pass
    
    def logic_logon_success_slot(self):
        self.logon_dlg.btn_logon.setText(u"已登陆")
        self.dlg.hide()                
    
    def logic_logon_failed_slot(self):
        self.logon_dlg.btn_logon.setText(u"登陆")
        self.logon_dlg.btn_logon.setEnabled(True)
        self.dlg.show()
        
    def add_cfg_users(self, cfgusers):
        self.cfgusers = cfgusers        
        for user in cfgusers.keys():            
            self.logon_dlg.combo_username.addItem(user)
        self.logon_dlg.combo_username.setCurrentIndex(0)
        self.on_choose_user_item(0)        
            
    def on_choose_user_item(self, index):
        key = str(self.logon_dlg.combo_username.itemText(index))
        if (self.cfgusers.has_key(key)):
            userinfo = self.cfgusers[key]            
            self.logon_dlg.edit_password.setText(userinfo["password"])
            self.logon_dlg.edit_deviceid.setText(userinfo["deviceid"])            
        
        
        